using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Collections.ObjectModel;

namespace GetSharePointToValidate.Deployment
{
    public class RemoveWebConfigModificationFromWebconfig
    {
        public void RemoveConfiguration(SPWebApplication WebApplication, string Owner)
        {
            if (WebApplication != null)
            {
                Collection<SPWebConfigModification> collection = WebApplication.WebConfigModifications;

                int count = collection.Count;

                for (int i = count - 1; i >= 0; i--)
                {
                    SPWebConfigModification webConfigModification = collection[i];

                    if (webConfigModification.Owner == Owner)
                        collection.Remove(webConfigModification);
                }

                if (count > collection.Count)
                {
                    WebApplication.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
                    WebApplication.Update();
                }
            }
        }

    }
}
